<?php
namespace app\common\command;

use addons\xiservice\controller\WeixinMini;
use app\api\controller\xiservice\Time;
use app\common\model\xiservice\Refuse;
use app\common\model\xiservice\User as XiserviceUser;
use app\common\model\xiservice\Wxtemplate;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\console\input\Option;

class Task extends Command
{
    protected function configure()
    {
        $this->setName('service5min')
            ->setDescription('家政服务5分钟定时任务');
    }

    public function execute(Input $input, Output $output)
    {

        $this->markorder($output);
        $this->remindorder($output);
        $this->cancelorder($output);
        $this->cancelconfirmorder($output);
        $this->qualityorder($output);
        $this->finishorder($output);
        $this->unfreeze($output);
    }

    /**
     * 标记异常订单
     * 下单多少分钟 没人接单则标记异常订单 后台处理
     */
    private function markorder($output)
    {
        $model = new \app\common\model\xiservice\Order;
        $minutes = isset(config('xiservice.order')['auto_mark_minutes']) ? config('xiservice.order')['auto_mark_minutes'] : 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '20')->where('unusual', 0)->where('createtime', '<', $expiretime)->select();
        $ids = [];
        foreach ($list as $key => $value) {
            $res = $model->where('id', $value['id'])->update(['unusual' => 1]);
            $ids[] = $value['id'];
        }

        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }
        $output->writeln('标记异常订单成功' . $idsString );
    }

    //超时提醒发送
    private function remindorder($output)
    {
        $model = new \app\common\model\xiservice\Order;
        $msgmodel = new \app\common\model\xiservice\Message;
        $minutes = 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '30')->where('send_remind', 0)->where('service_start_time', '<', $expiretime)->select();
        $ids = [];
        foreach ($list as $key => $value) {
            $workermsg = $msgmodel->sendOrderMessage($value, 5);
            $usermsg = $msgmodel->sendRemindUserMessage($value);
            $res = $model->where('id', $value['id'])->update(['send_remind' => 1]);
            $ids[] = $value['id'];
        }

        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }
        $output->writeln('超时提醒发送成功' . $idsString );
    }

    /**
     * 取消未支付订单
     * 下单多少分钟 未支付 则取消订单
     */
    private function cancelorder($output)
    {
        $model = new \app\common\model\xiservice\Order;
        $minutes = isset(config('xiservice.order')['auto_close_minutes']) ? config('xiservice.order')['auto_close_minutes'] : 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '10')->where('createtime', '<', $expiretime)->select();
        $ids = [];
        foreach ($list as $key => $value) {
            $res = $model->changeOrderStatus($value['id'], 'cancel');
            $ids[] = $value['id'];
        }
        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }
        $output->writeln('取消未支付订单成功' . $idsString );
    }

    /**
     * 取消订单
     * 下单多少分钟 已支付 服务人员未接单 则取消订单 =>超时处理
     */
    private function cancelconfirmorder($output)
    {
        $wx = new WeixinMini();
        $temp = new Wxtemplate();
        $model = new \app\common\model\xiservice\Order;
        $minutes = isset(config('xiservice.order')['auto_close_minutes']) ? config('xiservice.order')['auto_close_minutes'] : 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '20')->where('chose_type', 1)->where('pay_status', '20')->where('createtime', '<', $expiretime)->select();

        $ids = [];
        foreach ($list as $key => $value) {
            $res = $model->changeOrderStatus($value['id'], 'cancel');

            $order = $model->where('id', $value['id'])->find();
            //通知用户
            $openid = XiserviceUser::where('user_id', $order['user_id'])->value('mini_openid');
            $message = $temp->messageData($order, $openid, 'order_cancel');
            if ($message) {
                $result = $wx->subscribeMessage($message);
            }
            //超时处理
            $refuse = Refuse::insertGetId(['user_id' => $order['worker_id'], 'order_id' => $value['id'], 'createtime' => time()]);

            $ids[] = $value['id'];
        }

        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }
        $output->writeln('取消订单成功' . $idsString );
    }


    /**
     * 服务中的订单 24小时后自动进入质保
     */
    private function qualityorder($output)
    {
        $model = new \app\common\model\xiservice\Order;
        $day = isset(config('xiservice.order')['auto_receipt_days']) ? config('xiservice.order')['auto_receipt_days'] : 1;
        $expiretime = time() - $day * 24 * 60 * 60;
        $list = $model->where('order_status', '40')->where('pay_status', '20')->where('service_start_time', '<', $expiretime)->select();
        $ids = [];
        foreach ($list as $key => $value) {
//            $res = $model->changeOrderStatus($value['id'],'finished');
            $res = $model->changeOrderStatus($value['id'], 'quality');
            $ids[] = $value['id'];
        }

        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }
        $output->writeln('订单确认进入质保成功' . $idsString );
    }

    /**
     * 质保到期转完成订单
     */
    private function finishorder($output)
    {
        $model = new \app\common\model\xiservice\Order;
        $list = $model->where('order_status', '70')->where('quality_end_time', '<', time())->select();
        $ids = [];
        foreach ($list as $key => $value) {
            $res = $model->changeOrderStatus($value['id'], 'finished');
            $ids[] = $value['id'];
        }

        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }
        $output->writeln('质保到期订单自动完成成功' . $idsString );
    }

    /**
     * 解冻
     */
    private function unfreeze($output)
    {
        $benfitmodel = new \app\common\model\xiservice\Benfit;
        $day = isset(config('xiservice.order')['auto_freeze_days']) ? config('xiservice.order')['auto_freeze_days'] : 1;
        $expiretime = time() - $day * 24 * 60 * 60;
        $list = $benfitmodel->where('type', 1)->where('status', 1)->where('expiretime', '<', time())->select();
        $ids = [];
        foreach ($list as $key => $value) {
            $res = $benfitmodel->unfreeze($value);
            $ids[] = 'id' . $value->id . '=>u' . $value->user_id;
        }

        $idsString = '';
        if($ids) {
            $idsString = ', ids: ' . implode(',', $ids);
        }

        $output->writeln('资金解冻成功' . $idsString );
    }
}